<?php
class ControllerRoute{
	const REQUEST_GET = "GET";
	const REQUEST_POST = "POST";
	const SCRIPT_NAME = "index.php";
	const URL_TYPE_QUERY = "QUERY_STRING";
	const URL_TYPE_URI = "REQUEST_URI";
	const HTTP_PORT = 80;
	const HTTPS_PORT = 443;

	private $_moduleList = array(Configuration::MODULE_ADMIN);

	private $_serverUrl;
	private $_baseUrl;
	private $_requestUri;
	private $_queryString;

	/**
	 * Module name ...
	 * @var string
	 */
	private $_module;
	private $_controller;
	private $_action;
	private $_view;
	private $_viewType;
	/**
	 * Request Parram ...
	 * @var ArrayObject
	 */
	private $_parram;

	public function __construct(){
		$this->_init();
//		$this->_initRoute();
	}


	private function _initAdministratorRoute(){
		switch (count($this->_parram)) {
			case 3:
				$this->adminRoute3();;
				break;
			case 2:
				$this->adminRoute2();;
				break;
			default:
				$this->adminRoute0();
				break;
		}
	}
	/*-----------------------ADMINISTRATOR---------------------------------*/

	private function adminRoute0(){
		$this->_controller = Configuration::CONTROLLER_DEFAULT;
		$this->_action = Configuration::ACTION_DEFAULT;
		$this->_view = Configuration::VIEW_DEFAULT;
		$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
	}
	private function adminRoute2(){
		if(isset($this->_parram[1])){
			$this->_controller = $this->_parram[1];
		}
		else {
			$this->_controller = Configuration::CONTROLLER_DEFAULT;
		}
		if(isset($this->_parram[2]))
			$this->_action = $this->_parram[2];
		else
			$this->_action = Configuration::ACTION_DEFAULT;
		$this->_view = Configuration::VIEW_DEFAULT;
		$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
	}
	private function adminRoute3(){
		if(isset($this->_parram[1])){
			$this->_controller = $this->_parram[1];
		}
		else {
			$this->_controller = Configuration::CONTROLLER_DEFAULT;
		}
		if(isset($this->_parram[2]))
			$this->_action = $this->_parram[2];
		else
			$this->_action = Configuration::ACTION_DEFAULT;
		if(isset($this->_parram[3])){
			$view = $this->_parram[3];
			$arr = explode(".",$view);
			if(count($arr) == 2){
				if(isset($arr[0]))
				$this->_view = $arr[0];
				else
				$this->_view = Configuration::VIEW_DEFAULT;
				if(isset($arr[1])){
					$this->_viewType = $arr[1];
					$subarr = explode("?", $this->_viewType);
					if(count($subarr)>0){
						$this->_viewType = $subarr[0];
					}
				}
				else
				$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
			}
			else {
				$this->_view = Configuration::VIEW_DEFAULT;
				$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
			}
		}
		else {
			$this->_view = Configuration::VIEW_DEFAULT;
			$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
		}
	}
	/*-------------------------END Administrator Route--------------------------------------------------------------------------------------------*/
	private function _initFrontRoute(){
		switch (count($this->_parram)) {
			case 3:
				$this->frontRoute3();
			break;
			
			default:
				$this->frontRoute0();
			break;
		}
	}
	private function frontRoute0(){
		$this->_controller = Configuration::CONTROLLER_DEFAULT;
		$this->_action = Configuration::ACTION_DEFAULT;
		$this->_view = Configuration::VIEW_DEFAULT;
		$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
	}
	private function frontRoute3(){
		if(isset($this->_parram[0])){
			$this->_controller = $this->_parram[0];
		}
		else {
			$this->_controller = Configuration::CONTROLLER_DEFAULT;
		}
		if(isset($this->_parram[1]))
		$this->_action = $this->_parram[1];
		else
		$this->_action = Configuration::ACTION_DEFAULT;
		if(isset($this->_parram[2])){
			$view = $this->_parram[2];
			$arr = explode(".",$view);
			if(count($arr)==2){
				if(isset($arr[0]))
				$this->_view = $arr[0];
				else
				$this->_view = Configuration::VIEW_DEFAULT;
				if(isset($arr[1])){
					$this->_viewType = $arr[1];
					$subarr = explode("?", $this->_viewType);
					if(count($subarr)>0){
						$this->_viewType = $subarr[0];
					}
				}
				else
				$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
			}
			else{
				$this->_view = Configuration::VIEW_DEFAULT;
				$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
			}
		}
		else {
			$this->_view = Configuration::VIEW_DEFAULT;
			$this->_viewType = Configuration::VIEW_TYPE_DEFAULT;
		}
	}
	/*-------------------------END Front Route--------------------------------------------------------------------------------------------*/




	private function _init(){
		$port = $_SERVER['SERVER_PORT'];
		$this->_queryString = $_SERVER['QUERY_STRING'];
		if($port == ControllerRoute::HTTP_PORT || $port == ControllerRoute::HTTPS_PORT){
			$port = STRING_EMPTY;
		}
		else{
			$port = ":".$port;
		}
		$this->_serverUrl = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$port;
		$documentFolder = str_replace(ControllerRoute::SCRIPT_NAME, STRING_EMPTY, $_SERVER['SCRIPT_NAME']);
		$this->_baseUrl = $this->_serverUrl . $documentFolder;
		$this->_requestUri = substr($_SERVER['REQUEST_URI'], (strlen($documentFolder) - 1));
		$this->_parram = explode(SLASH, substr($this->_requestUri, 1));
	}
	private function _initRoute(){
		if(in_array($this->_parram[0], $this->_moduleList)){
			$this->_module = $this->_parram[0];
			unset($this->_parram[0]);
			$this->_initAdministratorRoute();
		}
		else{
			$this->_module = Configuration::MODULE_DEFAULT;
			$this->_initFrontRoute();
		}
	}
	public function fileRequest($name,$default = null){
		if(isset($_FILES[$name])){
			return $_FILES[$name]; 
		}
		else{
			return $default;
		}
	}
	public function request($name,$default = null){
		if(isset($_REQUEST[$name])){
			return $_REQUEST[$name]; 
		}
		else{
			return $default;
		}
	}
	
	public function postRequest($name,$default = null){
		if(isset($_POST[$name])){
			return $_POST[$name]; 
		}
		else{
			return $default;
		}
	}
	
	public function getRequest($name,$default = null){
		if(isset($_GET[$name])){
			return $_GET[$name]; 
		}
		else{
			return $default;
		}
	}

	public function fileRequests(){
		return $_FILES;
	}
	public function requests(){
		return $_REQUEST;
	}
	
	public function postRequests(){
		return $_POST;
	}
	
	public function getRequests(){
		return $_GET;
	}
	
	public function getServerUrl(){
		return $this->_serverUrl;
	}
	public function getBaseUrl(){
		return $this->_baseUrl;
	}

	public function getModule(){
		return $this->_module;
	}
	public function getController(){
		return $this->_controller;
	}
	public function getAction(){
		return $this->_action;
	}
	public function getView(){
		return $this->_view;
	}
	public function getViewType(){
		return $this->_viewType;
	}
	public function getQueryString(){
		return $this->_queryString;
	}
	public function getParram(){
		return $this->_parram;
	}
	public function getRequestUri(){
		return $this->_requestUri;
	}
}